﻿/*
    Copyright 2010, 2011 Eric Wong
	contact@optimalcycling.com
	http://www.optimalcycling.com
	Optimal Cycling - Advanced power pacing program for cyclists
	
	This file is part of Optimal Cycling.

    Optimal Cycling is free software: you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Optimal Cycling is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public License
    along with Optimal Cycling.  If not, see <http://www.gnu.org/licenses/>.
*/

using System;
using System.IO;
using System.Text;
using System.Threading;
using System.Globalization;
using System.Runtime.InteropServices;
using OptimalCycling.FileIO;
using OptimalCycling.FileIO.OC;
using CommandLineHelper;
using Cloo;

namespace OptimalCycling
{
    /// <summary>
    /// Outputs the version information to the console.
    /// </summary>
    class DisplayInfo
    {
        public static void DisplayVersion()
        {
            Console.WriteLine("<------------------------------------------------------>");
            Console.WriteLine("Optimal Cycling Kernel 0.9.1");
            Console.WriteLine("Calculates the optimal power pacing for cyclists.");
            Console.WriteLine("Copyright (c) 2010, 2011 Eric Wong.");
            Console.WriteLine("http://www.optimalcycling.com");
            Console.WriteLine("contact@optimalcycling.com");
            Console.WriteLine("<------------------------------------------------------>");
            Console.WriteLine();
        }
    }

    class Program
    {
        [DllImport("kernel32.dll")]
        static extern bool AllocConsole();

        [DllImport("kernel32.dll")]
        static extern bool FreeConsole();

        static void Main(string[] args)
        {
            // Used to workaround a bug with AMD's implementation of OpenCL in Windows where a console window
            // is required or the drivers won't run the OpenCL code.
            bool runningWin32NT = (Environment.OSVersion.Platform == PlatformID.Win32NT);
            bool consoleAllocated = false;

            // Put the following code before InitializeComponent() if there is a WinForms GUI.
            // Ensures the program always uses "." as the decimal point no matter the language on the user's computer.
            Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;
            Thread.CurrentThread.CurrentUICulture = CultureInfo.InvariantCulture;
            
            try
            {
                if (runningWin32NT) consoleAllocated = AllocConsole();
                DisplayInfo.DisplayVersion();
                Arguments commandLine = new Arguments(args);
                new OCSolve().Run(commandLine);
            }
            catch (Exception e)
            {
                Console.WriteLine("Error!");
                Console.WriteLine("\n---Source---");
                Console.WriteLine(e.Source);
                Console.WriteLine("\n---TargetSite---");
                Console.WriteLine(e.TargetSite);
                Console.WriteLine("\n---Message---");
                Console.WriteLine(e.Message);
                Console.WriteLine("\n---InnerException---");
                Console.WriteLine(e.InnerException);
                Console.WriteLine("\n---Stack Trace---");
                Console.WriteLine(e.StackTrace);
            }

            if (consoleAllocated) FreeConsole();

            Console.ReadKey();

        }
    }
}
